package com.tedu.parkinglot.chargeRule.service.impl;

import com.tedu.parkinglot.chargeRule.mapper.ChargeRuleMapper;
import com.tedu.parkinglot.chargeRule.pojo.dto.ChargeRuleQuery;
import com.tedu.parkinglot.chargeRule.pojo.dto.ChargeRuleSaveParam;
import com.tedu.parkinglot.chargeRule.pojo.entity.ChargeRule;
import com.tedu.parkinglot.chargeRule.pojo.vo.ChargeRuleVO;
import com.tedu.parkinglot.chargeRule.service.ChargeRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChargeRuleServiceImpl implements ChargeRuleService {

    private static final Logger log = LoggerFactory.getLogger(ChargeRuleServiceImpl.class);
    @Autowired
    private ChargeRuleMapper chargeRuleMapper;
    @Override
    public List<ChargeRuleVO> selectRules(ChargeRuleQuery query) {
        return chargeRuleMapper.select(query);
    }

    @Override
    public void saveRule(ChargeRuleSaveParam param) {
        ChargeRule chargeRule = new ChargeRule();
        BeanUtils.copyProperties(param, chargeRule);
        chargeRule.setUpdateTime(new Date());
        //chargeRule.setParkingId(666L);
        chargeRule.setRule("每小时"+param.getPrice()+"元，每满24小时"+param.getFeeLimit()+"元。");
        if (chargeRule.getId() == null) {
            chargeRule.setCreateTime(new Date());
            log.debug("新增计费规则"+chargeRule);
            chargeRuleMapper.insert(chargeRule);
        }else {
            log.debug("更新计费规则"+chargeRule);
            chargeRuleMapper.update(chargeRule);
        }
    }

    @Override
    public void deleteRule(Long id) {
       chargeRuleMapper.delete(id);
    }
}
